import {observable,action} from 'mobx';
import * as SearchAPI from '../apis/search'
import {WeaForm, WeaSwitch} from 'comsMobx';
import {WeaTableNew} from 'comsMobx'
const {TableStore} = WeaTableNew;
import {WeaLocaleProvider} from 'ecCom';
import { exportExcel } from '../apis/dialog';
const getLabel = WeaLocaleProvider.getLabel;

export default class Search {
    @observable userList = [];
    @observable loading =  false;
    @observable conditioninfo = [];
    @observable searchParamsAd = {};
    @observable showSearchAd = false;
    @observable fields = {};
    @observable dataKey = "";
    @observable topTab = [];
    @observable previewMeetingId = '';
    @observable dialogZIndex = 801;


    @observable tableStore = new TableStore();
    // @observable tableStore = {};

    //高级搜素的form
    @observable adSearchForm = new WeaForm();

    constructor() {

    }

    @action
    addUser(params) {
        this.userList.push({name: "dyf"});
    }

    getUserList(params) {
        return this.userList.slice();
    }

    @action doLoading = params => {
        
            // type: types.SEARCH_LOADING,
        //     loading:true,
        //     params
        // });


        this.loading = true;
        //调用API
        SearchAPI.getDatas(params).then(action((data) => {
            this.tableStore = {...this.tableStore, [data.sessionkey]:new TableStore()}
            this.tableStore[data.sessionkey].getDatas(data.sessionkey,  1);
            // this.tableStore.getDatas(data.sessionkey,  1);           
                // type: types.SEARCH_LOADING,
                // loading:false,
                // searchParamsAd: {...params },
                // dataKey: data.sessionkey
                this.loading = false; 
                this.dataKey = data.sessionkey
        }));

    }

    //初始化会议查询页查询条件
    @action initDatas = params => {
        SearchAPI.getCondition(params).then(action((data) => {
            const { groupinfo, pagetitle, conditioninfo } = data;
            this.adSearchForm = new WeaForm();
            this.adSearchForm.initFormFields(conditioninfo);


            // return {...state,conditioninfo:action.conditioninfo,topTab:action.topTab};
            //     type:types.SEARCH_CONDITION,
            //     conditioninfo: conditioninfo,
            //     topTab:data.groupinfo
            // });

            this.conditioninfo = conditioninfo;
            this.topTab = data.groupinfo;
        }));
    }

    //高级搜索受控
    @action setShowSearchAd = bool =>{
        // type: types.SET_SHOW_SEARCHAD_SEARCH,value:bool})
        this.showSearchAd = bool;
    }

    //高级搜索表单内容
    @action saveFields = (value = {}) => {
        // type: types.SAVE_FIELDS_SEARCH,value:value})

        this.fields = value;

        this.searchParamsAd = function(){

            let params = {};
            if(value){
                for (let key in value) {
                    params[value[key].name] = value[key].value
                }
            } 

            return params
        }();
    }


    @action savePreviewMeetingId = (value={}) => {
        // type: types.SEARCH_SAVEPREVIEWMEETINGID, data:value.meetingid});
        this.previewMeetingId = value.meetingid;
    }

    @action setZIndex = (value = '') => {
        // type: types.SEARCH_SETZINDEX, data:value});
        this.dialogZIndex = value;
    }

    @action exportExcel(dataKey) {
        this.tableStore[dataKey].exportAll();
    }

}